package com.laxcen.toydemo;

import android.content.Context;
import android.text.TextWatcher;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RadioGroup;
import android.widget.TextView;


import androidx.recyclerview.widget.RecyclerView;

import java.io.File;
import java.util.ArrayList;
import java.util.List;


/**
 * Created by Administrator on 2017/8/9.
 */

public abstract class BaseAdapter extends RecyclerView.Adapter<BaseAdapter.BaseViewHolder> {

    private Context mContext;
    private LayoutInflater mInflater;
    private List mData;  //数据源
    private int mLayoutId;  //布局id
    private File file;

    public BaseAdapter(Context context, List data, int layoutId) {
        mContext = context;
        mData = data;
        mLayoutId = layoutId;
        mInflater = LayoutInflater.from(context);
    }

    public BaseAdapter(Context context, Object[] data, int layoutId) {
        mContext = context;
        if (data != null) {
            mData = new ArrayList();
            for (int i = 0; i < data.length; i++) {
                mData.add(data[i]);
            }
        }
        mLayoutId = layoutId;
        mInflater = LayoutInflater.from(context);
    }

    public interface ImageChoose {
        void afterChoose(String path);
    }

    @Override
    public BaseViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new BaseViewHolder(mInflater.inflate(mLayoutId, parent, false));
    }

    @Override
    public void onBindViewHolder(BaseViewHolder holder, int position) {
        setWidget(holder, position);
    }

    @Override
    public int getItemViewType(int position) {
        return super.getItemViewType(position);

    }

    @Override
    public int getItemCount() {
        return null == mData ? 0 : mData.size();
    }

    protected abstract void setWidget(BaseViewHolder holder, int position);

    public class BaseViewHolder extends RecyclerView.ViewHolder {
        private View mView;
        private SparseArray<View> mViews;   //视图集合

        public BaseViewHolder(View itemView) {
            super(itemView);
            mViews = new SparseArray<>();
            mView = itemView;
        }

        /*根据控件id获取视图*/
        private View getView(int widgetId) {
            View view = mViews.get(widgetId);
            if (view == null) {
                view = mView.findViewById(widgetId);
                mViews.put(widgetId, view);
            }
            return view;
        }

        /*设置文字控件*/
        public BaseViewHolder setText(int widgetId, String str) {
            ((TextView) getView(widgetId)).setText(str);
            return this;
        }

        /*设置本地图形控件*/
        public BaseViewHolder loadImage(int widgetId, int resorceId) {
            ((ImageView) getView(widgetId)).setImageResource(resorceId);
            return this;
        }

        /*设置控件点击监听*/
        public BaseViewHolder setClickListner(int widgetId, View.OnClickListener listner) {
            getView(widgetId).setOnClickListener(listner);
            return this;
        }

        /*设置控件长按监听*/
        public BaseViewHolder setLongClickListner(int widgetId, View.OnLongClickListener listener) {
            getView(widgetId).setOnLongClickListener(listener);
            return this;
        }

        /*设置Enable属性*/
        public BaseViewHolder setEnable(int widdgetId, boolean enable) {
            getView(widdgetId).setEnabled(enable);
            return this;
        }

        /*设置空间可见性*/
        public BaseViewHolder setVisible(int widgetId, int visible) {
            getView(widgetId).setVisibility(visible);
            return this;
        }

        /*设置背景*/
        public BaseViewHolder setBackGroundResource(int widgetId, int resourceId) {
            getView(widgetId).setBackgroundResource(resourceId);
            return this;
        }

        /*设置字体颜色*/
        public BaseViewHolder setTextColor(int widgetId, int color) {
            ((TextView) getView(widgetId)).setTextColor(color);
            return this;
        }

//        /*设置图片选择器*/
//        public BaseViewHolder setImgaeChoose(int widgetId,ImageChoose imageChoose) {
//            RoundedImageView iv = (RoundedImageView) getView(widgetId);
//            MyImageLoad.init(mContext);
//            RxGalleryFinal
//                    .with(mContext)
//                    .image()
//                    .radio()
//                    .cropAspectRatioOptions(0, new AspectRatio("头像裁切", 10, 10))
//                    .crop()
//                    .imageLoader(ImageLoaderType.UNIVERSAL)
//                    .subscribe(new RxBusResultDisposable<ImageRadioResultEvent>() {
//                @Override
//                protected void onEvent(ImageRadioResultEvent imageRadioResultEvent) throws Exception {
//
//                }
//            })
//                    .openGallery();
//
//            //裁剪图片的回调
//            RxGalleryListener
//                    .getInstance()
//                    .setRadioImageCheckedListener(
//                            new IRadioImageCheckedListener() {
//                                @Override
//                                public void cropAfter(Object t) {
//                                    String path = t.toString();
////                                    Bitmap bitmap = BitmapFactory.decodeFile(path);
////                                    iv.setImageBitmap(bitmap);
//                                    imageChoose.afterChoose(path);
//                                }
//                                @Override
//                                public boolean isActivityFinish() {
//                                    return true;
//                                }
//                            });
//            return this;
//        }

        /*加载网络图*/
//        public BaseViewHolder loadNetworkImg(int widgetId, String url){
//            ImageView iv = (ImageView) getView(widgetId);
//            MyImageLoad.init(mContext);
//            MyImageLoad.imageLoader.displayImage(url,iv,MyImageLoad.headImg);
//            return this;
//        }

        /*添加单选按钮监听*/
        public BaseViewHolder setOnCheckChangeListener(int widgetId, RadioGroup.OnCheckedChangeListener listener) {
            RadioGroup rg = (RadioGroup) getView(widgetId);
            rg.setOnCheckedChangeListener(listener);
            return this;
        }

        /*设置是否可选*/
        public BaseViewHolder setEnabled(int widgetId, boolean enabled) {
            View view = getView(widgetId);
            view.setEnabled(enabled);
            return this;
        }

        /*设置输入框监听*/
        public BaseViewHolder setTextChange(int widgetId, TextWatcher textWatcher) {
            EditText editText = (EditText) getView(widgetId);
            editText.addTextChangedListener(textWatcher);
            return this;
        }

    }
}
